package Runde2;

import java.util.*;

public class LVAManager {
	
    private static HashMap<Integer, LVA> LVAMap = new HashMap<Integer, LVA>();
    
    /**
     * createUE - creates lva type ue
     * createVO - creates lva type vo
     * createVU - creates lva type vu
     * 
     * @param lvaNr - unique lva number
	 * @param title - lva title
	 * @param owner - owner of lva
	 * @param earliestReg - earliest date to register  
	 * @param latestReg - latest date to register
	 * @param deReg - latest date to deregister
	 * @param maxMembers - maximum amount of members. if unlimited value is integer.MAX_VALUE
	 * @param requirements - requirements students should meet 
     * @throws InvalidLVAException - numerous error cases (end of registration precede it's beginning, ...)
     */
    public static int createUE(int lvaNr, String title, Professor owner, GregorianCalendar earliestReg, GregorianCalendar latestReg, GregorianCalendar deReg, int maxMembers,  ArrayList<Integer> predecessors, int[] requirements) throws InvalidLVAException {
        
        try {
            if (LVAMap.containsKey(lvaNr)) {
                throw new InvalidLVAException("LVA already exists");
            } 
            LVA newLVA = new UE(lvaNr, title, owner, earliestReg, latestReg, deReg, maxMembers, predecessors, requirements);
            LVAMap.put(lvaNr, newLVA);
            return lvaNr;
        } catch (InvalidLVAException e) {
            throw new InvalidLVAException(e.getMessage());
        }
    }
    
   
    public static int createVO(int lvaNr, String title, Professor owner, GregorianCalendar earliestReg, GregorianCalendar latestReg, GregorianCalendar deReg, int maxMembers,  ArrayList<Integer> predecessors, int[] requirements) throws InvalidLVAException {
        try {
            if (LVAMap.containsKey(lvaNr)) {
                throw new InvalidLVAException("LVA already exists");
            } 
            LVA newLVA = new VO(lvaNr, title, owner, earliestReg, latestReg, deReg, maxMembers, predecessors, requirements);
            LVAMap.put(lvaNr, newLVA);
            return lvaNr;
        } catch (InvalidLVAException e) {
            throw new InvalidLVAException(e.getMessage());
        }
    }
    
    
    public static int createVU(int lvaNr, String title, Professor owner, GregorianCalendar earliestReg, GregorianCalendar latestReg, GregorianCalendar deReg, int maxMembers,  ArrayList<Integer> predecessors, int[] requirements) throws InvalidLVAException {
        try {
            if (LVAMap.containsKey(lvaNr)) {
                throw new InvalidLVAException("LVA already exists");
            } 
            LVA newLVA = new VU(lvaNr, title, owner, earliestReg, latestReg, deReg, maxMembers, predecessors, requirements);
            LVAMap.put(lvaNr, newLVA);
            return lvaNr;
        } catch (InvalidLVAException e) {
            throw new InvalidLVAException(e.getMessage());
        }
    }
    
    /**
     * get-method for UEs
     * 
     * @param lvaNr - number of the lva
     * @return - UE with lvaNr
     * @throws InvalidLVAException - lva is not UE
     */
    public static UE getUE(int lvaNr) throws InvalidLVAException {
        if (LVAMap.get(lvaNr) instanceof UE) {
            return (UE) LVAMap.get(lvaNr);
        } else {
            throw new InvalidLVAException("Wrong LVA type.");
        }
    }
    
    /**
     * get-method for VOs
     * 
     * @param lvaNr - number of the lva
     * @return - VO with lvaNr
     * @throws InvalidLVAException - lva is not VO
     */
    public static VO getVO(int lvaNr) throws InvalidLVAException {
        if (LVAMap.get(lvaNr) instanceof VO) {
            return (VO) LVAMap.get(lvaNr);
        } else {
            throw new InvalidLVAException("Wrong LVA type.");
        }
    }
    
    /**
     * get-method for VUs
     * 
     * @param lvaNr - number of the lva
     * @return - VU with lvaNr
     * @throws InvalidLVAException - lva is not VU
     */
    public static VU getVU(int lvaNr) throws InvalidLVAException {
        if (LVAMap.get(lvaNr) instanceof VU) {
            return (VU) LVAMap.get(lvaNr);
        } else {
            throw new InvalidLVAException("Wrong LVA type.");
        }
    }
    
    /**
     * Getter-function for LVAs in general.
     * 
     * @param lvaNr
     * @return LVA with lvaNr as its number.
     */
    public static LVA getLVA(int lvaNr) {
    	return LVAMap.get(lvaNr);
    }
    
    /**
     * Readable output of LVAList.
     */
    public static String output() {
        String output = "";
        for (LVA lva : LVAMap.values()) {
        	if (lva.getStatus() == false) {
        		output += lva;
        	}
        }
        return output;
    }
    
}